#include "GLButton.h"
GLButton::GLButton(int x, int y, UIInputHandler bIn, ClickHandler bPress, const char *str, int w, int h)
          :GLRect(x, y, w, h, 10)
{
    //using namespace GLui;
    bText = new TextObject(g_GLuiFont);
    bText->setText(str);
    bText->setColor(0, 0, 0); //default black
    sRect = new GLRect(x, y, w, h);

    //these handlers have to be initialized seperately.
    bInputHandler = bIn;
    bPressHandler = bPress;
    calledEventHandler = false;
}

void GLButton::show()
{
    int x = -1;
    int y = -1;
    bool clicked = false;

    if(bInputHandler)
    {
        setColor(255, 255, 255);
        bText->setColor(0, 0, 0);
        bInputHandler(x, y, clicked);
        if(isPointOver(x, y))
        {
            if(clicked)
            {
                if(!calledEventHandler)
                {
                    bPressHandler();
                    calledEventHandler = true;
                }
            }
            else
            {
                if(calledEventHandler)
                    calledEventHandler = false;
                setColor(11, 92, 142);
                bText->setColor(255, 255, 255);
            }
        }
    }
    else
    {
        printf("handlers not set for button:\n");//, bText->getText());
    }
 
    glDisable(GL_TEXTURE_2D);  
    draw();
    glEnable(GL_TEXTURE_2D);
    bText->drawText(getX() + getWidth()/2.0f - bText->getW()/2.0f, getY()-5.0f);

}
GLButton::~GLButton()
{
    delete bText;
    delete sRect;
}
